/**
 * 
 */
//普通搜索通过关键字匹配fileinfo中的id,name,version,department,creator,project信息
//var normalSearch=new Ext.FormPanel({
//	title : '普通搜索',
//	blankText : '不允许为空',
//	labelAlign : 'right',
//	frame : true,
//	items : [{
//		id : 'searchInfo_txt',
//		xtype : 'textfield',
//		name : 'searchInfo',
//		fieldLabel : '搜索关键字',
//		allowblank : false,
//		enableKeyEvents : true
//    }, {
//    	xtype : 'button',
//		height : 20,
//		width : 50,
//		text : '搜索',
//		labelAlign : 'right',
//		listeners : {
//			'click' : function() {
//				//激活页面
//				Ext.getCmp('content-panel').layout.setActiveItem('search-panel');
//				//重新设置store的proxy的url
//				var c=Ext.getCmp('searchInfo_txt');
//				searchResultStore.proxy=new Ext.data.HttpProxy({
//					url:'showSearchFileList.action?method=showSearchFileList&keyWord='+c.getValue(),
//					method:'post'
//				});
//				//调用search中定义的function
//				showSearchResult();
//			}
//		}}
//	]
//});
var detailSearch=new Ext.FormPanel({
	title:'搜索',
	blankText:'不允许为空',
	labelAlign:'right',

	items:[{
			id:'searchFileName_txt',
			xtype:'textfield',
			name:'searchFileName',
			fieldLabel:'文件名',
			allowblank:false,
			enableKeyEvents:true
	    },{
		    id:'searchProject_txt',
			xtype:'textfield',
			name:'searchProject',
			fieldLabel:'项目',
			allowblank:false,
			enableKeyEvents:true	    	    	 
	    },
	    departmentCombo
	    ,{
		    id:'searchVersion_txt',
			xtype:'textfield',
			name:'searchVersion',
			fieldLabel:'版本',
			allowblank:false,
			enableKeyEvents:true		    	    	 
	    },{
		    id:'searchCreator_txt',
			xtype:'textfield',
			name:'searchCreator',
			fieldLabel:'公司',
			allowblank:false,
			enableKeyEvents:true		    	    	 
	    },{
		    id:'searchType_cmb',
		    name:'searchType',
			xtype:'combo',
			fieldLabel:'类型',
			allowblank:true,
			emptyText: '请选择',
			mode: 'local',   
	        triggerAction: 'all',   
	        valueField: 'id',   
	        displayField: 'text',   
	        editable:false,  
	        selectOnFocus : false,  
	        forceSelection : false,
			store: new Ext.data.SimpleStore({   
	            fields: ['id','text'],
	            data: [['1','流程模型']
	            		,['2','功能模型']
	            		,['3','组织模型']
	            		,['4','业务对象模型']
	            		,['5','epc模型']
						,['6','权限模型']
						,['7','角色模型']]   
	        })
	    }, {
	    	xtype : 'button',
			height : 20,
			width : 50,
			text : '搜索',
			listeners : {
				'click' : function() {
					
					//激活页面
					Ext.getCmp('content-panel').layout.setActiveItem('search-panel');
					//重新设置store的proxy的url
					var cName=Ext.getCmp('searchFileName_txt');
					var cProject=Ext.getCmp('searchProject_txt');
					//var cDepartment=Ext.getCmp('searchDepartment_cmb');
					var cVersion=Ext.getCmp('searchVersion_txt');
					var cCreator=Ext.getCmp('searchCreator_txt');
					var cType=Ext.getCmp('searchType_cmb');
					
					//cCreator = new String(URLDecoder.decode(cCreator).getBytes("ISO-8895-1"),"UTF-8");
					//ar a = java.net.URLEncoder.endoce(cCreator.getValue(),"utf-8");
					
					searchResultStore.proxy=new Ext.data.HttpProxy({
						url:'showDetailSearchFileList.action?method=showDetailSearchFileList&keyWord1='+encodeURIComponent(cName.getValue())
							+'&keyWord2='+encodeURIComponent(cProject.getValue())
							+'&keyWord3='+departmentCombo.getValue()
							+'&keyWord4='+encodeURIComponent(cVersion.getValue())
							+'&keyWord5='+encodeURIComponent(cCreator.getValue())
							+'&keyWord6='+cType.getValue(),
						method:'post'
					});
					departmentCombo.clearValue();
					Ext.getCmp('searchType_cmb').clearValue();
					//调用search中定义的function
					showSearchResult();
				}
			}}
		
	]
});
//searchPanel here
var searchPanel = new Ext.TabPanel({
	title : '模型搜索',
	activeTab : 0,
	autoHeight:true,
	items : [
//	    normalSearch,
	    detailSearch
	]
});


var searchResultStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({	//proxy从哪里获取数据
		url : 'showSearchFileList.action?method=showSearchFileList&keyWord=',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({	//reader如何解析数据
		root : 'File',
		totalProperty : 'totalFile',
		id : 'id'
	}, [ {
		name : 'id'
	}, {
		name : 'name'
	}, {
		name : 'project'
	}, {
		name : 'department'
	}, {
		name : 'version'
	}, {
		name : 'xmlpath'
	}, {
		name : 'company'
	}, {
		name : 'type'
	} ])
});

function showSearchResult(){
	searchResultStore.load();
}

var searchResultGrid=new Ext.grid.GridPanel({	
	
	region:'center',
	ds : searchResultStore,	//绑定store
	enableColumnResize : true,
	border : true,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	
	cm : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : '文件名',
		width : 160,
		dataIndex : 'name',
		sortable : false,
		fixed : true
	},{
		header : '项目',
		width : 130,
		dataIndex : 'project',
		sortable : false,
		fixed : true
	},{
		header : '部门',
		width : 130,
		dataIndex : 'department',
		sortable : false,
		fixed : true
	},{
		header : '版本',
		width : 130,
		dataIndex : 'version',
		sortable : false,
		fixed : true
	},{
		header : '类型',
		width : 130,
		dataIndex : 'type',
		sortable : false,
		fixed : true,
		renderer:function(value, cellmeta, record){
			var typemap=[['1','流程模型']
					,['2','功能模型']
					,['3','组织模型']
					,['4','业务对象模型']
					,['5','epc模型']
					,['6','权限模型']
					,['7','角色模型']];
			return typemap[value-1][1];
		}
	}])
});

var searchGridPanel=new Ext.Panel({
	title:'搜索结果',
	items:[searchResultGrid],
	tbar:[
	{
		//删除选中文件按钮
		xtype:'button',
		enabled:false,
		iconCls : 'search',
		width:120,
		text:'查看模型',
		handler:displayModel
	},
    {xtype : "tbseparator"},
	{
		//删除选中文件按钮
		xtype:'button',
		enabled:false,
		iconCls : 'db-icn-cross',
		width:120,
		text:'删除模型',
		handler:deleteFile
	}]
});

var search = new Ext.Panel({
	    id: 'search-panel',
	    title: '搜索结果页',
	    layout: 'fit',
	    showPage:function(){
	    	
	    },
	    items :[ 
	        searchPanel,
	        searchGridPanel     
	    ]
});

//删除选中的模型文件
function deleteFile(){
	var sm=searchResultGrid.getSelectionModel();
	var record=sm.getSelections()[0];
	if(record==null){
		return;
	}
	authenticate(record.get('id'),'delete',function(){
		Ext.Msg.confirm("删除模型","确认要删除选中的模型文件吗？",function (btn){ 
			if (btn == 'yes'){ 
				Ext.Ajax.request({
					url: 'deleteFileInfo.action?method=deleteFileInfo&id='+record.get('id'),				   	
				   	success: function(res,opt){			   		
				   		searchResultStore.remove(record);		   		
				   	},
				   	failure: function(){
				   		Ext.Msg.alert('消息','您没有删除文件的权限。');
				   	}
				});
			}
		});
	});	
}

//显示模型视图
function displayModel(){	
	var sm=searchResultGrid.getSelectionModel();
	var record=sm.getSelections()[0];
	if(record==null){
		return;
	}
	authenticate(record.get('id'),'read',function(){
		var text=record.get('id');
		var typemap=[['1','process']
					,['2','function']
					,['3','department']
					,['4','domain']
					,['5','epc']
					,['6','permission']
					,['7','role']];
		var type=record.get('type');
		var pathName=typemap[type-1][1];
		Ext.Ajax.request({
			method:'POST',
			url:'showXmlContentAction',
			params : {
						xmlName:text,
						pathName:pathName
					  },
			success : function(request) {
			
				xmldata = request.responseText;
		
				test=xmldata.replace(/lineChange/g,lineChange);
				change=test.replace(/slash/g,"/");
				finalformat=change.replace(/quotation/g,"\"");
				finalformat=finalformat.replace("{"+"\""+"xmlContent"+"\""+":"+"\"","");
				finalformat=finalformat.replace("\""+"}","");
				Ext.getCmp('xmltextarea').setValue(finalformat);
				modelWin.show();
			}
			
		});
		
		var modelWin=new Ext.Window({
			title : '查看模型',
			width : 700,
			height : 300,
			resizable : true,
			modal:true,
			layout : 'fit',
			closeAction: 'hide',             //这个很重要，为了防止关闭窗口后无法打开
			items : [{
				xtype:'textarea',
				id:'xmltextarea',
				region : 'center',
				autoScroll : true,				
				border : true
	    	}
	//		    new Ext.Panel({
	//		    	items:[{
	//					xtype:'textarea',
	//					id:'xmltextarea',
	//					region : 'center',
	//					autoScroll : true,				
	//					border : true
	//		    	}]
	//		    })
			]	
		});	
	});
}